package com.timothy.gfund.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.markupartist.android.widget.PullToRefreshListView;
import com.sina.stock.SinaStockClient;
import com.sina.stock.SinaStockInfo;
import com.timothy.junk.R;
import com.timothy.gfund.bean.Repo;
import com.timothy.junk.MainActivity;
import com.timothy.junk.adapter.RepoAdaptor;
import com.timothy.junk.common.parse.RepoParser;
import com.timothy.junk.common.util.NetWorkUtil;
import com.timothy.junk.gfund.bean.GFundBean;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class RepoList extends ListActivity {
	public static final String LOG_TAG = "RepoList";

    Context mContext;
    RepoAdaptor adaptor ;
    ImageButton homeBtn;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.repo_list);
        
        homeBtn = (ImageButton) findViewById(R.id.homeBtn);
        homeBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent=new Intent(mContext, MainActivity.class);
				startActivity(intent);
			}
		});
        
        mContext = getApplicationContext();
        // Set a listener to be invoked when the list should be refreshed.
        ((PullToRefreshListView) getListView()).setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Do work to refresh the list here.
                new GetDataTask().execute();
            }
        });
        if(NetWorkUtil.isNetworkAvailable(mContext)){
        	mClient = SinaStockClient.getInstance();
        	new GetDataTask().execute();
        }else{
        	Toast.makeText(mContext, "Your network is not available!", Toast.LENGTH_LONG).show();
        }
    }

    SinaStockClient mClient;
    List<String> codeList;
    List<Repo> repoList;
    List<Repo> repoValueList;
    private class GetDataTask extends AsyncTask<Void, Void, String[]> {
        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(2000);
                loadData();
            } catch (InterruptedException e) {
            }
            return null;
        }
        @Override
        protected void onPostExecute(String[] result) {
            super.onPostExecute(result);
            adaptor = new RepoAdaptor(mContext, repoValueList);
            setListAdapter(adaptor);
            // Call onRefreshComplete when the list has been refreshed.
            ((PullToRefreshListView) getListView()).onRefreshComplete();

        }
    }

    public void loadData(){
        codeList = RepoParser.getRepoCodeList(mContext);
//        Map<String,Double> codeValueMap = null; //code&info Map
        Map<String,SinaStockInfo> codeStockMap = null;
        try {
            //��ȡ֤ȯ�������Ϣ��key:���� Value ��Ϣ��
            String[] array = new String[codeList.size()];
            int i=0;
            for(String code: codeList){
            	array[i++] = code;
            }
            codeStockMap = mClient.getStockInfoMap(array);

            repoValueList = new ArrayList<Repo>();
            repoList = RepoParser.getRepoList(mContext);
            for (Repo repo : repoList) {
                String code= repo.getCode();
                SinaStockInfo ssInfo = codeStockMap.get(code);
                repo.setPrice(ssInfo.getNowPrice());
                repoValueList.add(repo);
            }
        } catch (IOException e) {
            Log.e(LOG_TAG, e.getMessage());
        } catch (SinaStockInfo.ParseStockInfoException e) {
            Log.e(LOG_TAG, e.getMessage());
        }
    }

    public String reposNeedNotify(List<Repo> repoValueList){
        StringBuffer sb = new StringBuffer();
        for (Repo repo : repoValueList) {
            if(repo.getPrice() >= 4){
                sb.append( (sb.length()>0) ? (","+repo.getName()): repo.getName());
            }
        }
        return sb.toString();
    }
}
